package tk.xboot.flink.kfk.json2;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.formats.json.JsonNodeDeserializationSchema;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;

import java.io.InputStream;
import java.util.Properties;

public class KafkaJsonMain {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        Properties props = new Properties();
        try (InputStream is = KafkaJsonMain.class.getClassLoader().getResourceAsStream("kfk-consumer.properties")) {
            props.load(is);
        } catch (Exception e) {
            System.out.println(e);
            System.exit(-1);
        }

        FlinkKafkaConsumer011 consumer = new FlinkKafkaConsumer011<>(props.getProperty("topic"),
                new JsonNodeDeserializationSchema(), props);
        consumer.assignTimestampsAndWatermarks(new MessageWaterEmitter());
        // consumer return type of ObjectNode
        DataStreamSource<ObjectNode> dataStream = env.addSource(consumer);
        // return a SingleOutputStreamOperator<T> object.
        DataStream<Tuple2<String, Long>> soso = dataStream.map(new MapFunction<ObjectNode, Tuple2<String, Long>>() {
            @Override
            public Tuple2<String, Long> map(ObjectNode value) throws Exception {
                if (value != null && value.get("cnt") != null && value.get("coin") != null) {
                    return new Tuple2<>(value.get("coin").textValue(), value.get("cnt").longValue());
                }
                return null;
            }
        });

        // add watermark
        /*DataStream<Tuple2<String, Long>> watermarkDS = soso
                .assignTimestampsAndWatermarks(new AssignerWithPeriodicWatermarks<Tuple2<String, Long>>() {
                    Long currentMaxTimestamp = 0L;
                    Long maxOutOfOrderness = 10000L;//最大允许的乱序时间是10s
                    Watermark watermark = null;
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

                    @Nullable
                    @Override
                    public Watermark getCurrentWatermark() {
                        watermark = new Watermark(currentMaxTimestamp - maxOutOfOrderness);
                        return watermark;
                    }

                    @Override
                    public long extractTimestamp(Tuple2<String, Long> element, long previousElementTimestamp) {

                        return 0;
                    }
                });*/

        // grouping and apply.
        //watermarkDS.keyBy(0)
        soso.keyBy(0)
                .window(TumblingEventTimeWindows.of(Time.seconds(1)))
                .maxBy(1).print();
//                .apply(new WindowFunction<Tuple2<String, Long>, Object, Tuple, TimeWindow>() {
//                    /**
//                     * Evaluates the window and outputs none or several elements.
//                     *
//                     * @param tuple  The key for which this window is evaluated.
//                     * @param window The window that is being evaluated.
//                     * @param input  The elements in the window being evaluated.
//                     * @param out    A collector for emitting elements.
//                     * @throws Exception The function may throw exceptions to fail the program and trigger recovery.
//                     */
//                    @Override
//                    public void apply(Tuple tuple, TimeWindow window, Iterable<Tuple2<String, Long>> input, Collector<Object> out) throws Exception {
//                        System.out.println(tuple);
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//                        out.collect("window  " + format.format(window.getStart()) + "   window  " + format.format(window.getEnd()));
//                    }
//                }).print();

//        dataStream.map(new MapFunction<ObjectNode, Tuple2<Long, String>>() {
//
//            /**
//             * The mapping method. Takes an element from the input data set and transforms
//             * it into exactly one element.
//             *
//             * @param value The input value.
//             * @return The transformed value
//             * @throws Exception This method may throw exceptions. Throwing an exception will cause the operation
//             *                   to fail and may trigger recovery.
//             */
//            @Override
//            public Tuple2<Long, String> map(ObjectNode value) throws Exception {
//                return new Tuple2<>(value.get("cnt").longValue(), value.get("coin").textValue());
//            }
//
//            private static final long serialVersionUID = -6867736771747690202L;
//        })
//                .keyBy(1)
//                .timeWindow(Time.seconds(10), Time.seconds(3))
//                .maxBy(0);

        //dataStream.print().setParallelism(1);

        env.execute();
    }

}
